﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

// InputDataSource(name[", description"])
//    ConnectionString = "<connection_string>"
//    [SelectQuery = "<select_query>"]
//    [UpdateQuery = "<update_query>"]
//    [UseInputAsOutput = True | False"]
//    [JoinKey = "<variable_name>"]
//    [JoinType = "Full" | "Inner" | "Left"]
//    [JoinKeySorted = True | False"]
// End InputDataSource
public class InputDataSourceSection : DmsSectionWithDescription
{
    private DmsProperty? _connectionStringProperty;
    private DmsProperty? _joinKeyProperty;
    private DmsProperty? _joinKeySortedProperty;
    private DmsProperty? _joinTypeProperty;
    private DmsProperty? _selectQueryProperty;
    private DmsProperty? _updateQueryProperty;
    private DmsProperty? _useInputAsOutputProperty;

    internal InputDataSourceSection(SyntaxToken declareKeywordToken, Identifier name, StringLiteral? description,
        SyntaxNode? parent) :
        base(SyntaxKinds.InputDataSourceSection, declareKeywordToken, name, description, parent)
    {
    }

    /// <summary>
    ///     Dms 属性 : ConnectionString = "connection_string"
    /// </summary>
    public DmsProperty? ConnectionStringProperty => _connectionStringProperty;

    /// <summary>
    ///     Dms 属性 : SelectQuery = "select_query"
    /// </summary>
    public DmsProperty? SelectQueryProperty => _selectQueryProperty;

    /// <summary>
    ///     Dms 属性 : UpdateQuery = "update_query"
    /// </summary>
    public DmsProperty? UpdateQueryProperty => _updateQueryProperty;

    /// <summary>
    ///     Dms 属性 : JoinKey = "variable_name"
    /// </summary>
    public DmsProperty? JoinKeyProperty => _joinKeyProperty;

    /// <summary>
    ///     Dms 属性 : JoinType = "Full" | "Inner" | "Left"
    /// </summary>
    public DmsProperty? JoinTypeProperty => _joinTypeProperty;
    
    /// <summary>
    ///     Dms 属性 : UseInputAsOutput = True | False"
    /// </summary>
    public DmsProperty? UseInputAsOutputProperty => _useInputAsOutputProperty;

    /// <summary>
    ///     Dms 属性 : JoinKeySorted = True | False"
    /// </summary>
    public DmsProperty? JoinKeySortedProperty => _joinKeySortedProperty;


    public void Update(DmsProperty? connectionString,
        DmsProperty? selectQuery,
        DmsProperty? updateQuery,
        DmsProperty? joinKey,
        DmsProperty? joinType,
        DmsProperty? useInputAsOutput,
        DmsProperty? joinKeySorted)
    {
        SetChildNullableField(ref _connectionStringProperty, connectionString);
        SetChildNullableField(ref _selectQueryProperty, selectQuery);
        SetChildNullableField(ref _updateQueryProperty, updateQuery);
        SetChildNullableField(ref _joinKeyProperty, joinKey);
        SetChildNullableField(ref _joinTypeProperty, joinType);
        SetChildNullableField(ref _useInputAsOutputProperty, useInputAsOutput);
        SetChildNullableField(ref _joinKeySortedProperty, joinKeySorted);
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, DeclareKeywordToken, NameIdentifier, Description,
            ConnectionStringProperty, SelectQueryProperty, UpdateQueryProperty,
            JoinKeyProperty, JoinTypeProperty, UseInputAsOutputProperty,
            JoinKeySortedProperty);
    }
}


public static partial class NodeFactory
{
    public static InputDataSourceSection CreateInputDataSourceSection(SyntaxNode? parent,
        SyntaxToken declareKeywordToken, Identifier identifierName, StringLiteral? description)
    {
        Debug.Assert(declareKeywordToken.IsNode(SyntaxKinds.InputDataSourceKeywordToken));
        return new InputDataSourceSection(declareKeywordToken, identifierName, description, parent);
    }
}